/// faichou  2017-01-05  关于地址的请求
/// 1. 获取所有地址
/// 2. 添加地址
/// 3. 更新地址, 设为默认地址
/// 4. 删除地址

// var app = getApp()
// console.log(app.globalData.encrptdatadata.userid)
// var userId = app.globalData.encrptdatadata.userid
// var userToken = app.globalData.encrptdatadata.usertoken
var util = require('../../utils/util.js')

var BaseUrl = util.baseUrl
// var getAllAddrUrl = BaseUrl + '/c2mwebservice/api/v1/members/' + userId + '/addresses'
// var addAddrUrl = BaseUrl + '/c2mwebservice/api/v1/members/' + userId + '/addresses'
// var delAddrUrl = BaseUrl + '/c2mwebservice/api/v1/members/' + userId + '/addresses/'
// var editAddrUrl = BaseUrl + '/c2mwebservice/api/v1/members/' + userId + '/addresses/'
/// 1. 获取地址
function requestAllAddr(success, fail){
	if (typeof success != 'function' || typeof fail != 'function') {
		return
	}
  let app = getApp()
	wx.request({
	  //必需
	  url: BaseUrl + '/c2mwebservice/api/v1/members/' + app.globalData.mohuanUser.memberId + '/addresses',
	  method: 'GET',
	  header: {
	      'Content-Type': 'application/json',
	      'C2M-Identify':'Platform:iOS,APP Version:6.300,OS Version:10.2,Device Type:xiaochengxu',
	      'Content-Encoding':'gzip',
	      'X-AUTH-TOKEN': app.globalData.mohuanUser["X-AUTH-TOKEN"]
	  },
	  success: function(res) {
	    if (res.data.returnCode == 200) {
	    	success(res.data)
	    } else {
	    	fail(res.data.reason)
	    }
	  },
	  fail: function(res) {
	    fail('网络错误')
	  },
	  complete: function(res) {
	    
	  }
	})
}

/// 2. 添加地址
function addAddress(address, success, fail){
  if( typeof success != 'function' || typeof fail != 'function' ) {
    return
  }
  let app = getApp()
  wx.request({
    //必需
    url: BaseUrl + '/c2mwebservice/api/v1/members/' + app.globalData.mohuanUser.memberId + '/addresses',
    data: address,
    method: 'POST',
    header: {
        'Content-Type': 'application/json',
        'C2M-Identify':'Platform:iOS,APP Version:6.300,OS Version:10.2,Device Type:xiaochengxu',
        'Content-Encoding':'gzip',
	    'X-AUTH-TOKEN': app.globalData.mohuanUser["X-AUTH-TOKEN"]
    },
    success: function(res) {
      if (res.data.returnCode == 200) {
      	success(res.data)
      } else {
      	fail(res.data.reason)
      }
    },
    fail: function(res) {
      fail('网络错误')
    },
    complete: function(res) {
      
    }
  })
}

/// 3. 删除地址
function delAddress(addrId, success, fail){
  if( typeof success != 'function' || typeof fail != 'function' ) {
    return
  }
  let app = getApp()
  wx.request({
    //必需
    url: BaseUrl + '/c2mwebservice/api/v1/members/' + app.globalData.mohuanUser.memberId + '/addresses/'+addrId,
    method: 'DELETE',
    header: {
        'Content-Type': 'application/json',
        'C2M-Identify':'Platform:iOS,APP Version:6.300,OS Version:10.2,Device Type:xiaochengxu',
        'Content-Encoding':'gzip',
	    'X-AUTH-TOKEN': app.globalData.mohuanUser["X-AUTH-TOKEN"]
    },
    success: function(res) {
      if (res.data.returnCode == 200) {
      	success()
      } else {
      	fail('删除地址错误')
      }
    },
    fail: function(res) {
      fail('网络错误')
    },
    complete: function(res) {
      
    }
  })
}

/// 4. 修改地址
function editAddress(addrId, address, success, fail){
  if( typeof success != 'function' || typeof fail != 'function' ) {
    return
  }
  // console.log(editAddrUrl+addrId)
  // console.log(address)
  let app = getApp()
  wx.request({
    //必需
    url: BaseUrl + '/c2mwebservice/api/v1/members/' + app.globalData.mohuanUser.memberId + '/addresses/'+addrId,
    method: 'POST',
    data: address,
    header: {
        'Content-Type': 'application/json',
        'C2M-Identify':'Platform:iOS,APP Version:6.300,OS Version:10.2,Device Type:xiaochengxu',
        'Content-Encoding':'gzip',
	    'X-AUTH-TOKEN': app.globalData.mohuanUser["X-AUTH-TOKEN"]
    },
    success: function(res) {
      if (res.data.returnCode == 200) {
      	success()
      } else {
      	fail()
      }
    },
    fail: function(res) {
      fail()
    },
    complete: function(res) {
      
    }
  })
}



module.exports = {
	requestAllAddr:requestAllAddr,
	addAddress: addAddress,
	editAddress: editAddress,
	delAddress: delAddress
}

